package org.acsis.jee.dao.hibernate;

import org.acsis.jee.dao.ClientDAO;
import org.acsis.jee.dao.DAOFactory;
import org.acsis.jee.dao.LieuDAO;
import org.acsis.jee.dao.TrajetDAO;
import org.acsis.jee.dao.UtilisateurDAO;
import org.acsis.jee.dao.VehiculeDAO;
import org.acsis.jee.util.HibernateUtil;
import org.apache.log4j.Logger;
import org.hibernate.Session;

public class HibernateFactory extends DAOFactory{

	private static Logger log = Logger.getLogger(HibernateFactory.class);
	
	@Override
	public ClientDAO getClientDAO() {
		return (ClientDAO) instantiateDAO(ClientDAOHibernate.class);
	}

	@Override
	public LieuDAO getLieuDAO() {
		return (LieuDAO) instantiateDAO(LieuDAOHibernate.class);
	}

	@Override
	public TrajetDAO getTrajetDAO() {
		return (TrajetDAO) instantiateDAO(TrajetDAOHibernate.class);
	}

	@Override
	public UtilisateurDAO getUtilisateurDAO() {
		return (UtilisateurDAO) instantiateDAO(UtilisateurDAOHibernate.class);
	}

	@Override
	public VehiculeDAO getVehiculeDAO() {
		return (VehiculeDAO) instantiateDAO(VehiculeDAOHibernate.class);
	}
	
	private BasicHibernateDAO<?,?> instantiateDAO(Class<?> daoClass) {
        try {
        	BasicHibernateDAO<?,?> dao = (BasicHibernateDAO<?,?>)daoClass.newInstance();
            dao.setSession(getCurrentSession());
            return dao;
        } catch (Exception ex) {
        	log.info(ex.getMessage());
            throw new RuntimeException("Can not instantiate DAO: " + daoClass, ex);
        }
	}
	 
	protected Session getCurrentSession() {
        return HibernateUtil.getSession();
    }
	
}
